import json
import logging

import pytest
import requests
def du_dl():
    with open("dl.json","r",encoding="utf8")as f:
        data=json.load(f)
        return data
class Test1():
    token = ""
    def setup_method(self):
        print("初始化环境")
    def teardown_method(self):
        print("恢复环境")
        #登录

    def test_dl(self):
        try:
            url="http://10.59.9.12:7001/api/public/login"
            body={"username":"13811033962","password":"123456"}
            r=requests.post(url=url,json=body)
            print("登录成功",r.json())
            logging.info(r.json())
            Test1.token= "Bearer "     +r.json()["data"]["token"]
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert '普通会员' in r.text
            assert  r.json()["data"]['group_name']=='普通会员'
    def test_sp(self):
        try:
            url="http://10.59.9.12:7001/api/goods/view?id=4"
            r=requests.get(url=url)
            print("查看商品详情",r.json())
            logging.info(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert "ok" in r.text
            assert  r.json()=="ok"

    @pytest.mark.parametrize("data", du_dl())
    def test_jrgwc(self,data):
        try:
            url="http://10.59.9.12:7001/api/cart/add"
            boby={"id":data.get("id"),"sku_id":"","num":data.get("num")}
            head={"auth-token":Test1.token}
            r=requests.post(url=url,json=boby,headers=head)
            print("加入购物车成功",r.json())
            logging.info(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert "商品添加至购物车成功" in r.text
            assert  r.json()=="商品添加至购物车成功"
    def test_ckgwclb(self):
        try:
            url="http://10.59.9.12:7001/api/cart?page=1"
            # boby={"page=1"}
            head={"auth-token":Test1.token}
            r=requests.get(url=url,headers=head)
            print("查看购物车列表",r.json())
            logging.info(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert "阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机" in r.text
            assert  r.json()["data"]["list"][0]=="阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机"
    def test_zhifu(self):
        try:
            url="http://10.59.9.12:7001/api/order/detail"
            boby={"id":"64463"}
            head={"auth-token":Test1.token}
            r=requests.post(url=url,json=boby,headers=head)
            print("支付成功",r.json())
            logging.info(r.json())
        except Exception as e:
            logging.info(e)
            assert r.status_code==200
            assert "河北省 石家庄市 新华区" in r.text
            assert  r.json()["data"]["region"][0]=="河北省 石家庄市 新华区"